﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Common.Exp2D
{
    public class Object2D
    {
        protected Color [] m_textureData;
        public Color [] TextureData
        {
            get {return m_textureData;}
        }        

        public Vector2 m_position;

        public virtual Vector2 Position
        {
            get { return m_position; }
            set { m_position = value; }
        }

        public Vector2 CenterPosition
        {
            get { return new Vector2 (m_position.X + m_texture.Width / 2, m_position.Y + m_texture.Height / 2);}
        }

        public Texture2D Texture
        {
            get { return m_texture; }
            set { m_texture = value; }
        }

        protected float m_scale = 1f;
        public float Scale
        {
            get { return m_scale; }
            set
            {
                m_scale = value;
            }
        }
        protected float m_depth = 0;
        protected Vector2 m_origin;
        protected float m_rotation;
        public float Rotation
        {
            get { return m_rotation; }
            set { m_rotation = value; }
        }
        protected Texture2D m_texture;
        public static readonly Color INITIAL_COLOR = Color.White;
        protected Color m_color;
        public Color Color
        {
            get { return m_color; }
            set { m_color = value; }
        }

        protected bool m_collidable = false;
        public bool Collidable
        {
            get { return m_collidable; }
            set { m_collidable = value; }
        }

        protected float m_mass = 6;
        public float Mass
        {
            get { return m_mass; }
            set { m_mass = value; }
        }

        public Object2D(Texture2D texture, Vector2 position, bool originAtCenter)
        {
            m_texture = texture;
            m_position = position;
            if (originAtCenter)
            {
                m_origin = new Vector2(texture.Width / 2, texture.Height / 2);
            }
            else
            {
                m_origin = Vector2.Zero;
            }
            m_color = INITIAL_COLOR;

            //set color data
            m_textureData = new Color[m_texture.Width * m_texture.Height];
            m_texture.GetData(m_textureData);
        }

        public static bool IntersectRectangle(Rectangle rectangleA, Rectangle rectangleB,out double intersectionSurface)
        {
            Rectangle rectResult;
            bool result = false;
            intersectionSurface = 0;
            Rectangle.Intersect(ref rectangleA,ref rectangleB, out rectResult);
            if (rectResult != null && rectResult != Rectangle.Empty)
            {
                result = true;
                intersectionSurface = rectResult.Width * rectResult.Height;
            }
            return result;

        }

        public double DistanceFrom(Vector2 objPosition)
        {
            float distance = Vector2.Distance(objPosition, Position);
            return distance;
        }

        public float DistanceFromCenter(Vector2 objPosition)
        {
            Vector2 center = new Vector2(Position.X + m_texture.Width / 2, m_position.Y + m_texture.Height / 2);
            float distance = Vector2.Distance(objPosition, center);
            return distance;
        }

        public static bool IntersectPixels(Rectangle rectangleA, Color[] dataA,
                                    Rectangle rectangleB, Color[] dataB)
        {
            // Find the bounds of the rectangle intersection
            int top = Math.Max(rectangleA.Top, rectangleB.Top);
            int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
            int left = Math.Max(rectangleA.Left, rectangleB.Left);
            int right = Math.Min(rectangleA.Right, rectangleB.Right);

            // Check every point within the intersection bounds
            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
                    // Get the color of both pixels at this point
                    Color colorA = dataA[(x - rectangleA.Left) +
                                         (y - rectangleA.Top) * rectangleA.Width];
                    Color colorB = dataB[(x - rectangleB.Left) +
                                         (y - rectangleB.Top) * rectangleB.Width];

                    // If both pixels are not completely transparent,
                    if (colorA.A != 0 && colorB.A != 0)
                    {
                        // then an intersection has been found
                        return true;
                    }
                }
            }

            // No intersection found
            return false;
        }
       

        public void  Update(GameTime gameTime)
        {
 	        
        }

        public virtual void Draw(GameTime time, SpriteBatch spriteBatch)
        {   
            spriteBatch.Draw(m_texture, m_position, null, m_color, m_rotation, m_origin, m_scale, SpriteEffects.None, m_depth);            
        }
    }
}
